package com.bilibili.server.service;

import com.bilibili.server.entity.User;
import com.bilibili.server.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class UserService {
    @Autowired
    private UserRepository userRepository;

    // 获取用户信息
    public User getUserInfo(String username) {
        Optional<User> userOptional = userRepository.findByUsernameAndStatus(username, 1);
        return userOptional.orElse(null);
    }

    // 编辑用户信息
    public User updateUserInfo(String username, User updatedUser) {
        Optional<User> userOptional = userRepository.findByUsernameAndStatus(username, 1);
        if (userOptional.isPresent()) {
            User user = userOptional.get();
            // 更新用户信息（只更新允许修改的字段）
            if (updatedUser.getNickname() != null) {
                user.setNickname(updatedUser.getNickname());
            }
            if (updatedUser.getAvatar() != null) {
                user.setAvatar(updatedUser.getAvatar());
            }
            return userRepository.save(user);
        }
        return null;
    }
}